﻿using System;
using System.Collections.Generic;
using Ur;
using Sargon;

using Pixels.Logic;

namespace Pixels.Rendering
{
    class WorldRenderer : Sargon.State
    {
        public Camera camera;

        public WorldRenderer() {
            camera = new Camera();
            register_method(Trigger.frame, frame);
            register_method(Trigger.pre_frame, input);
        }

        Canvas baseCanvas = new Canvas(0f);

        void frame() {

            baseCanvas.doSort = true;

            bool doCull = false;

            Render.canvas = baseCanvas;

            int left = 0, top = 0, right = 0, bottom = 0;

            var bottomRightWorld = camera.unproject(new crds2(0, 0));
            var topLeftWorld = camera.unproject(new crds2(Screen.w, Screen.h));

            if (doCull) {
                left = topLeftWorld.x.myfloor();
                top = topLeftWorld.y.myfloor();
                right = bottomRightWorld.x.myfloor() + 1;
                bottom = bottomRightWorld.y.myfloor() + 1;

                left = left.choke(0, World.Map.Width - 1);
                right = right.choke(0, World.Map.Width - 1);


            } else {
                left = 0; top = 0;
                right = World.Map.Width - 1;
                bottom = World.Map.Height - 1;
            }

            top = top.choke(0, World.Map.Height - 1);
            bottom = bottom.choke(0, World.Map.Height - 1);
            
            for (var iy = top;  iy <= bottom; iy++)
            for (var ix = left; ix <= right ; ix++) {
                var tile = World.Map[ix, iy];
                var sc = camera.project(tile.worldCenter);
                var spr = Render.spriteInBox("diamond", new Rect(sc.x, sc.y, Camera.FACTOR * camera.zoom * 2f, Camera.FACTOR * camera.zoom), true);
                //spr.color = GlobalConsts.getTerrainConsts(tile.Type).color;        
                spr.color = Color.grey(0.02f * tile.x);
                var txt = Render.textInBox("[" + ix + "," + iy + "]", new Rect(sc.x, sc.y, Camera.FACTOR, 30f), true);
                txt.zed = 100;
                
            }

        }


        void input() {
            if (Keys.Mouse3.held()) {
                var prevWC = camera.unproject( Mouse.coords - Mouse.delta);
                var currentWC = camera.unproject( Mouse.coords);
                var deltaWC = prevWC - currentWC;
                camera.coords = camera.target = camera.target + deltaWC;      
                
            }

            if (Mouse.wheel != 0) {
                camera.zoomGoal += Mouse.wheel * 0.1f;
                if (camera.zoomGoal > camera.maxZoom) camera.zoomGoal = camera.maxZoom;
                if (camera.zoomGoal < camera.minZoom) camera.zoomGoal = camera.minZoom;

                //camera.zoom = camera.zoomGoal;

            }

            if (camera.zoom > camera.zoomGoal) camera.zoom -= 0.5f * Game.frame_time;
            if (camera.zoom < camera.zoomGoal) camera.zoom += 0.5f * Game.frame_time;

        }
        

        void renderTile(Tile t) {
            camera.project(t.worldCenter);
        }
    }
}
